﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using AppManagement.Repositories;
using AppManagement.Helpers;
using AppManagement.Models;

namespace AppManagement.MainManagers
{
    public partial class FrmDanhSachHocPhan : DevComponents.DotNetBar.Office2007Form
    {
        public EventHandler Handler { get; set; }
        private DevComponents.DotNetBar.Controls.TextBoxX txtMaKhoa;
        private DataTable DataSource;

        public FrmDanhSachHocPhan()
        {
            InitializeComponent();
            InitHiddenFields();
            InitGridView();
        }

        public void InitHiddenFields()
        {
            txtMaKhoa = new DevComponents.DotNetBar.Controls.TextBoxX
            {
                Name = "txtMaKhoa",
                Text = string.Empty
            };
            txtMaKhoa.Visible = false;
        }

        private void btnChonKhoa_Click(object sender, EventArgs e)
        {
            FrmQuanLyKhoa frm = new FrmQuanLyKhoa(true);
            frm.Handler += GetKhoa;
            frm.ShowDialog();
        }

        public void GetKhoa(object sender, EventArgs e)
        {
            var eventType = (TransferSimpleDataEvent)e;
            string[] comp = eventType.Data.Split(new char[] { '#' });
            txtMaKhoa.Text = comp[0];
            txtKhoa.Text = comp[1];

            var lstChuyenNganh = ChuyenNganhRepository.SelectByMaKhoa(int.Parse(txtMaKhoa.Text));
            cbxChuyenNganh.Items.Clear();
            foreach (var item in lstChuyenNganh)
            {
                cbxChuyenNganh.Items.Add(item);
            }
            if (cbxChuyenNganh.Items.Count > 0)
            {
                cbxChuyenNganh.SelectedIndex = 0;
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void cbxChuyenNganh_SelectedIndexChanged(object sender, EventArgs e)
        {
            var chuyenNganh = (ChuyenNganh)cbxChuyenNganh.SelectedItem;
            var lstKHDT = KeHoachDaoTaoRepository.SelectByMaCN(chuyenNganh.MaCN);
            cbxKHDT.Items.Clear();
            foreach (var item in lstKHDT)
            {
                cbxKHDT.Items.Add(item);
            }
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            if (string.Empty.Equals(txtMaKhoa.Text))
            {
                MessageBoxEx.Show("Vui lòng nhập mã khoa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (cbxChuyenNganh.SelectedIndex == -1)
            {
                MessageBoxEx.Show("Vui lòng nhập chọn chuyên ngành", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (cbxKHDT.SelectedIndex == -1)
            {
                MessageBoxEx.Show("Vui lòng nhập chọn kế hoạch đào tạo", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            FrmQuanLyPhieuDangKy frm = new FrmQuanLyPhieuDangKy(int.Parse(txtMaKhoa.Text), ((ChuyenNganh)cbxChuyenNganh.SelectedItem).MaCN, ((KeHoachDaoTao)cbxKHDT.SelectedItem).MaKHDT);
            frm.ShowDialog();
        }

        private void InitGridView()
        {
            DataGridViewTextBoxColumn objColumn = new DataGridViewTextBoxColumn
            {
                HeaderText = "MaCTKHDT",
                DataPropertyName = "MaCTKHDT",
                Name = "MaCTKHDT",
                Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth) * ((double)1 / 3)) - 30
            };
            dtgvData.Columns.Add(objColumn);

            objColumn = new DataGridViewTextBoxColumn
            {
                HeaderText = "MaHP",
                DataPropertyName = "MaHP",
                Name = "MaHP",
                Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth) * ((double)1 / 3)) - 30
            };
            dtgvData.Columns.Add(objColumn);

            objColumn = new DataGridViewTextBoxColumn
            {
                HeaderText = "TenHP",
                DataPropertyName = "TenHP",
                Name = "TenHP",
                Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth) * ((double)1 / 3)) + 60 - 2
            };
            dtgvData.Columns.Add(objColumn);
        }

        public void InitDataSource()
        {
            DataSource = new DataTable();
            DataSource.Columns.Add("MaCTKHDT");
            DataSource.Columns.Add("MaHP");
            DataSource.Columns.Add("TenHP");
            DataView objTimeSheetDV = new System.Data.DataView(DataSource);
            objTimeSheetDV.AllowNew = false;
            dtgvData.AutoGenerateColumns = false;
            dtgvData.DataSource = objTimeSheetDV;
            dtgvData.ClearSelection();
        }

        private void cbxKHDT_SelectedIndexChanged(object sender, EventArgs e)
        {
            var khdt = KeHoachDaoTaoRepository.SelectByID(((KeHoachDaoTao)cbxKHDT.SelectedItem).MaKHDT);

            if (khdt != null)
            {
                var lstCTKHDT = ChiTietKHDTRepository.SelectByMaKHDT(khdt.MaKHDT);

                InitDataSource();

                foreach (var item in lstCTKHDT)
                {
                    DataSource.Rows.Add(item.MaCTKHDT, item.MaHP, item.HocPhan.TenHP);
                }
            }
        }

        private void dtgvData_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dtgvData.SelectedRows.Count > 0)
            {
                this.Hide();
                FrmDanhSachSinhVienDangKyHocPhan frm = new FrmDanhSachSinhVienDangKyHocPhan(int.Parse(txtMaKhoa.Text), ((ChuyenNganh)cbxChuyenNganh.SelectedItem).MaCN, ((KeHoachDaoTao)cbxKHDT.SelectedItem).MaKHDT, int.Parse(dtgvData.SelectedRows[0].Cells["MaCTKHDT"].Value.ToString()));
                frm.Handler += ShowFormAgain;
                frm.ShowDialog();
            }
        }

        private void ShowFormAgain(object sender, EventArgs e)
        {
            this.Show();
        }
    }
}